<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace model;

/**
 * Description of TblProductOrder
 *
 * @author Damian
 */
class TblProductOrder extends Model{
   /**
     * @column
     * @type int
     * @length 11
     * @primary
     * @readwrite
     */
   public $ProductOrderID;
   const ID="ProductOrderID";
   /**
    * @readwrite
    */
   public $OrderID;
   const ORDER_ID="OrderID";
   /**
    * @readwrite
    */
   public $ProductID;
   const PRODUCT_ID="ProductID";
   /**
    * @readwrite
    */
   public $Amount;
   const AMOUNT="Amount";
   public static function findRelatedProducts($productID){
       $orderIDs=TblOrder::findIDsByProductID($productID); 
       if (!$orderIDs){
           return false;
       }
       $criteria = new Criteria();
//        $criteria->from(TblProductOrder::getModel()->getTable())
//                ->where(TblProductOrder::PRODUCT_ID, "=", $id)->all();
        $all= $criteria->from(TblProductOrder::getModel()->getTable(),["Distinct(`tblproduct`.`ProductID`)"=>"tblproductorder_ProductID"])
                ->in(TblProductOrder::ORDER_ID, $orderIDs)
                ->where(TblProductOrder::PRODUCT_ID, "!=", $productID,  TblProductOrder::getModel()->getTable(), " and ")
                ->limit(10)
                ->join(TblProduct::getModel());
        $products = TblProductOrder::getModel()->all($criteria);
        if (sizeof($products)==0) {
            return false;
        }
        return $products;

        //var_dump($all);
    }
}
